package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Collection;
import com.softeem.music_sys.service.CollectionService;
import com.softeem.music_sys.mapper.CollectionMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author RD
* @description 针对表【collection(用户收藏表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection>
    implements CollectionService{

    @Override
    public Page<Collection> getCollectionsByUser(Integer userId, Integer page, Integer limit) {
        Page<Collection> pageUtil = new Page<>(page, limit);
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("collection_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Collection> getCollectionsByUserAndType(Integer userId, Object type, Integer page, Integer limit) {
        Page<Collection> pageUtil = new Page<>(page, limit);
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("type", type);
        wrapper.orderByDesc("collection_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public boolean isCollected(Integer userId, Object type, Integer targetId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        return count(wrapper) > 0;
    }

    @Override
    public long getCollectionCount(Object type, Integer targetId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        return count(wrapper);
    }

    @Override
    public boolean cancelCollection(Integer userId, Object type, Integer targetId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        return remove(wrapper);
    }
    
    @Override
    public Result addCollection(Collection collection) {
        // 检查是否已经收藏
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", collection.getUserId());
        wrapper.eq("type", collection.getType());
        wrapper.eq("target_id", collection.getTargetId());
        Collection exist = this.getOne(wrapper);
        if (exist != null) {
            return Result.fail("已经收藏过了");
        }
        
        // 设置收藏时间
        collection.setCollectionTime(new Date());
        
        boolean result = this.save(collection);
        return result ? Result.success().setMessage("收藏成功") : Result.fail("收藏失败");
    }
    
    @Override
    public Result deleteCollection(Integer id) {
        Collection collection = this.getById(id);
        if (collection == null) {
            return Result.fail("收藏记录不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }
    
    @Override
    public Result getCollectionsByUserController(Integer userId, Integer page, Integer limit) {
        Page<Collection> pageUtil = new Page<>(page, limit);
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("collection_time");
        Page<Collection> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getCollectionsByUserAndTypeController(Integer userId, Object type, Integer page, Integer limit) {
        Page<Collection> pageUtil = new Page<>(page, limit);
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("type", type);
        wrapper.orderByDesc("collection_time");
        Page<Collection> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result checkCollectionController(Integer userId, Object type, Integer targetId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        Collection collection = this.getOne(wrapper);
        boolean isCollected = collection != null;
        return Result.success().setData(isCollected);
    }
    
    @Override
    public Result getCollectionCountController(Object type, Integer targetId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        long count = this.count(wrapper);
        return Result.success().setData(count);
    }
    
    @Override
    public Result batchAddCollection(Integer userId, Object type, List<Integer> targetIds) {
        if (targetIds == null || targetIds.isEmpty()) {
            return Result.fail("收藏列表不能为空");
        }
        
        int successCount = 0;
        for (Integer targetId : targetIds) {
            // 检查是否已经收藏
            QueryWrapper<Collection> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            wrapper.eq("type", type);
            wrapper.eq("target_id", targetId);
            Collection exist = this.getOne(wrapper);
            if (exist != null) {
                continue;
            }
            
            Collection collection = new Collection();
            collection.setUserId(userId);
            collection.setType(type);
            collection.setTargetId(targetId);
            collection.setCollectionTime(new Date());
            
            if (this.save(collection)) {
                successCount++;
            }
        }
        
        return Result.success().setMessage("成功收藏" + successCount + "项");
    }
}